<template>
  <el-card class="staff-container">
    <el-table
      :data="allUserList"
      height="700"
      v-loading="loading"
      :element-loading-text="loadingText"
    >
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="mobile" label="手机号"></el-table-column>
      <el-table-column prop="password" label="密码"></el-table-column>
      <el-table-column prop="group" label="小组名称"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click.native.prevent="showDialog(scope.row.id)"
            >权限编辑</el-button
          >
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty :image-size="200" description="暂无业务员"></el-empty>
      </template>
    </el-table>
    <!-- 分页组件 -->
    <div style="height: 60px; margin-top: 25px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[15, 30, 40]"
        :page-size="page_size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 权限设置对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      title="设置权限"
      :visible.sync="dialogVisible"
      width="50%"
      center
    >
      <div class="select_section" style="margin-bottom: 20px">
        <span style="margin-right: 20px">请选择资质部门</span>
        <el-radio-group v-model="depart_id">
          <el-radio :label="1">资质一部</el-radio>
          <el-radio :label="2">资质二部</el-radio>
        </el-radio-group>
      </div>
      <span style="margin-right: 20px">请选择小组名称</span>
      <el-select ref="adminList" v-model="value" placeholder="请选择小组名称">
        <el-option
          v-for="item in adminList"
          :key="item.value"
          :label="item.group"
          :value="item.id"
        >
        </el-option>
      </el-select>
      <span slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="dialogVisible = false"
          >取 消</el-button
        >
        <el-button type="primary" @click.native.prevent="editRole"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import {
  getAllStaffListApi,
  getAdminListApi,
  editRoleApi,
  delUserApi
} from '@/api/user'
export default {
  name: 'staff-page',
  data () {
    return {
      page: 1,
      depart_id: 1,
      page_size: 15,
      total: 0,
      allUserList: [],
      loading: false,
      loadingText: '',
      dialogVisible: false,
      value: '',
      adminList: []
    }
  },
  created () {
    this.getAllStaffList()
  },
  methods: {
    // 获取 业务员 列表
    async getAllStaffList () {
      this.loading = true
      this.loadingText = '数据正在加载中!'
      const { data: res } = await getAllStaffListApi(this.page, this.page_size)
      // console.log('获取 业务员 列表', res)
      this.allUserList = [...res.data.lists]
      this.total = res.data.num
      this.loading = false
      this.loadingText = ''
    },
    // 获取 管理员 列表
    async getAdminList () {
      const { data: res } = await getAdminListApi()
      // console.log('获取 管理员 列表', res)
      this.adminList = [...res.data.zulist]
    },
    // 切换第几页
    handleCurrentChange (val) {
      this.page = val
      this.getAllStaffList()
    },
    // 手动更改页码
    handleSizeChange (val) {
      this.page_size = val
      this.getAllStaffList()
    },
    // 划分组员
    async editRole () {
      if (this.value === '') {
        return this.$message({
          showClose: true,
          message: '请选择修改的角色!',
          type: 'warning',
          duration: 1500
        })
      }
      this.dialogVisible = false
      this.loading = true
      this.loadingText = '正在修改角色!'
      const { data: res } = await editRoleApi(
        this.userid,
        this.value,
        this.depart_id
      )
      // console.log('修改用户角色', res)
      if (res.code === 200) {
        this.getAllStaffList()
        this.$message({
          showClose: true,
          message: '修改角色成功!',
          type: 'success',
          duration: 1500
        })
      }
      this.value = ''
      this.loading = false
      this.loadingText = ''
    },
    // 删除用户
    async delUser (val) {
      this.userid = val.id
      this.$confirm('你确定要删除吗？', '温馨提示', { type: 'warning' })
        .then(async () => {
          this.loading = true
          this.loadingText = '正在删除用户!'
          await delUserApi(this.userid)
          this.$message({
            showClose: true,
            message: '删除成功!',
            type: 'success',
            duration: 1500
          })
          this.getAllStaffList()
          this.userid = ''
          this.loading = false
          this.loadingText = ''
        })
        .catch(() => {})
    },
    // 打开对话框
    showDialog (val) {
      this.getAdminList()
      this.dialogVisible = true
      this.userid = val
    }
  },
  watch: {}
}
</script>

<style lang="scss" scoped>
.staff-container {
  box-sizing: border-box;
  height: 835px;
  padding: 25px 40px 0px;
  ::v-deep .el-dialog {
    .el-dialog__header {
      background-color: rgb(84, 92, 100);
      padding: 20px 20px 20px;
      .el-dialog__title {
        color: #fff;
      }
    }
  }
}
</style>
